//
//  MDHViewModel.m
//  JSTech
//
//  Created by 高得华 on 2019/5/24.
//  Copyright © 2020 华&梅. All rights reserved.
//

#import "MDHViewModel.h"

@interface MDHViewModel ()

/// 整个应用的服务层 The `services` parameter in `-initWithServices:params` method.
@property (nonatomic, strong, readwrite) id<MDHViewModelServices> services;
/// The `params` parameter in `-initWithServices:params` method.
@property (nonatomic, readwrite, copy) NSDictionary *params;
/// A RACSubject object, which representing all errors occurred in view model.
@property (nonatomic, readwrite, strong) RACSubject *errors;
/// The `View` willDisappearSignal
@property (nonatomic, readwrite, strong) RACSubject *willDisappearSignal;
///让 键盘 消失
@property (nonatomic, strong, readwrite) RACSubject * subKeyboardHidden;
///刷新某个单元格
@property (nonatomic, strong, readwrite) RACSubject * subRefreshCell;

@end

@implementation MDHViewModel

@synthesize subRefreshUI = _subRefreshUI;
@synthesize subKeyboardHidden = _subKeyboardHidden;
@synthesize subRefreshCell = _subRefreshCell;

/// when `BaseViewModel` created and call `initWithParams` method , so we can ` initialize `
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    MDHViewModel *viewModel = [super allocWithZone:zone];
    //@weakify(viewModel)
    __weak typeof(viewModel) weakVm = viewModel;
    [[weakVm
      rac_signalForSelector:@selector(initWithServices:params:)]
     subscribeNext:^(id x) {
         //@strongify(viewModel)
         [weakVm initialize];
     }];
    return viewModel;
}

/// create `viewModel` instance
- (instancetype)initWithServices:(id<MDHViewModelServices>)services params:(NSDictionary *)params {
    self = [super init];
    if (self) {
        self.user = services.client.currentUser;
        self.prefersNavigationBarBottomLineHidden = false;
        /// 默认在viewDidLoad里面加载本地和服务器的数据
        self.shouldFetchLocalDataOnViewModelInitialize = true;
        self.shouldRequestRemoteDataOnViewDidLoad = false;
        /// 允许IQKeyboardMananger接管键盘弹出事件
        self.keyboardEnable = false;
        self.keyboardEnableAutoToolbar = false;
        self.shouldResignOnTouchOutside = true;
        self.keyboardDistanceFromTextField = 10.0f;
        self.navigationBackgroundAlpha = 1.0;
        self.shouldHiddenBack = false;
        self.shouldHiddenBackTop = true;
        self.emptyVerticalOffset = 50;

        self.title = params[MHViewModelTitleKey];
        /// 赋值
        self.services = services;
        self.params   = params;
    }
    return self;
}

- (UIView *)view {
    if (!_view) {
        _view = [UIApplication sharedApplication].keyWindow;
    }
    return _view;
}
- (RACSubject *)errors {
    if (!_errors) _errors = [RACSubject subject];
    return _errors;
}
- (RACSubject *)subKeyboardHidden {
    if (!_subKeyboardHidden) _subKeyboardHidden = [RACSubject subject];
    return _subKeyboardHidden;
}

- (RACSubject *)subRefreshUI {
    if (!_subRefreshUI) _subRefreshUI = [RACSubject subject];
    return _subRefreshUI;
}

- (RACSubject *)subRefreshCell {
    if (!_subRefreshCell) _subRefreshCell = [RACSubject subject];
    return _subRefreshCell;
}

- (RACSubject *)willDisappearSignal {
    if (!_willDisappearSignal) _willDisappearSignal = [RACSubject subject];
    return _willDisappearSignal;
}

/// sub class can override
- (void)initialize {}


/// 适配iPhone X + iOS 11
+ (void)adjustsScrollViewInsets_Never:(UIScrollView *)scrollView {
    if (@available(iOS 11.0, *)) {
        do {
            if ([scrollView respondsToSelector:NSSelectorFromString(@"setContentInsetAdjustmentBehavior:")]) {
                NSMethodSignature *signature = [UIScrollView instanceMethodSignatureForSelector:@selector(setContentInsetAdjustmentBehavior:)];
                NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
                NSInteger argument = 2;
                invocation.target = scrollView;
                invocation.selector = @selector(setContentInsetAdjustmentBehavior:);
                [invocation setArgument:&argument atIndex:2];
                [invocation retainArguments];
                [invocation invoke];
            }
        } while (0);
    }
}



@end
